﻿
#if UNITY_EDITOR
    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public static class ScriptSyncServer
{
    
    [MenuItem("Codes/CoreData 同步到服务端")]
    static void SyncCodes_GameData()
    {
        string srcFolder = Application.dataPath + "/0A2_Scripts/Shared";
        string dstFolder =
            Application.dataPath.Substring(0, Application.dataPath.Length -
            "/ChatAppUnity/Assets".Length) +
            "/ChatAppServer/BattleData/Shared";

        SyncCodes(srcFolder, dstFolder);

        string srcFolder2 = Application.dataPath + "/0A2_Scripts/UtilityShared";
        string dstFolder2 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
            "/ChatAppUnity/Assets".Length) +
            "/ChatAppServer/UtilityShared/UtilityShared";

        SyncCodes(srcFolder2, dstFolder2);
        
        string srcFolder3 = Application.dataPath + "/0A2_Scripts/ServerData";
        string dstFolder3 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
                                              "/ChatAppUnity/Assets".Length) +
            "/ChatAppServer/ServerShared/ServerData";

        SyncCodes(srcFolder3, dstFolder3);
        

        ///刷新动画数据
        //const string animPrefabPath = "Assets/EditorAssets/AnimEditController.prefab";
        //var animPrefab = AssetDatabase.LoadAssetAtPath<AnimEditController>(animPrefabPath);

        //var anim = GameObject.Instantiate(animPrefab);
        //anim.LoadFBXAnimData();
        //anim.SaveData();

        //GameObject.DestroyImmediate(anim.gameObject);


        SyncData();
    }

/*    static void Sync2Robot()
    {
//同步到机器人工程
        string srcFolder2 = Application.dataPath + "/0A2_Scripts/Scene/Common/BehaviorTree";
        string dstFolder2 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
                                              "/Client/FrameSyncClient/Assets".Length) +
            "/PFRobot/PFRobot/BehaviorTree";

        SyncCodes(srcFolder2, dstFolder2);
        
        srcFolder2 = Application.dataPath + "/0A2_Scripts/Net";
        dstFolder2 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
                                              "/Client/FrameSyncClient/Assets".Length) +
            "/PFRobot/PFRobot/Net";

        SyncCodes(srcFolder2, dstFolder2);
        
                
        srcFolder2 = Application.dataPath + "/0A2_Scripts/Scene/Proto";
        dstFolder2 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
                                              "/Client/FrameSyncClient/Assets".Length) +
            "/PFRobot/PFRobot/Proto";

        SyncCodes(srcFolder2, dstFolder2);
    }*/
    
    /// <summary>
    /// 同步数据
    /// </summary>
    public static void SyncData()
    {
        string srcFolder3 = Application.dataPath + "/Resources/Data/Shared";
        string dstFolder3 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
            "/ChatAppUnity/Assets".Length) +
            "/ChatAppServer/ChatAppServer/bin/Debug/net8.0/Data/Shared";

        if (!Directory.Exists(dstFolder3))
            Directory.CreateDirectory(dstFolder3);

        string dstFolder4 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
        "/ChatAppUnity/Assets".Length) +
        "/ChatAppServer/ChatAppServer/Data/Shared";

        if (!Directory.Exists(dstFolder4))
            Directory.CreateDirectory(dstFolder4);

/*        string dstFolder6 =
            Application.dataPath.Substring(0, Application.dataPath.Length -
                                              "/Client/FrameSyncClient/Assets".Length) +
            "/FSServer/FrameSyncServer/ServerShared/bin/Debug/Data/Shared";

        if (!Directory.Exists(dstFolder6))
            Directory.CreateDirectory(dstFolder6);*/

        SyncCodes(srcFolder3, dstFolder3,false);
        SyncCodes(srcFolder3, dstFolder4,false);
        //SyncCodes(srcFolder3, dstFolder6);
    }


    //static void SyncTables()
    //{
    //    string srcFolder = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length) + "/DefaultTables";
    //    string dstFolder;

    //    dstFolder =
    //        Application.dataPath.Substring(0, Application.dataPath.Length -
    //                                          "/Client/Assets".Length) +
    //        "/Server/Config/DefaultTables";
    //    SyncCodes(srcFolder, dstFolder);
    //}

    //[MenuItem("Codes/Battle Data Core 同步到本地")]
    //static void SyncCodes_BattleCoreData_ToLocal()
    //{
    //    string srcFolder = Application.dataPath + "/Codes/Battle/1_CoreData/SharedCoreData";
    //    string dstFolder = null;

    //    // 本地BattleCoreData
    //    dstFolder =
    //        Application.dataPath.Substring(0, Application.dataPath.Length -
    //                                          "/Assets".Length) +
    //        "/BattleCoreData/SharedCoreData";
    //    if (!Directory.Exists(dstFolder))
    //    {
    //        Directory.CreateDirectory(dstFolder);
    //    }

    //    SyncCodes(srcFolder, dstFolder);

    //}


    public static void SyncCodes(string srcFolder, string dstFolder,bool clearTargetDir = true)
    {
        if (!Directory.Exists(srcFolder))
        {
            Debug.LogError("源文件夹不存在: " + srcFolder);
            return;
        }

        if (!Directory.Exists(dstFolder))
        {
           // Debug.Log(string.Format("目标文件夹不存在:{0}", dstFolder));
            Directory.CreateDirectory(dstFolder);
        }

        if (Directory.Exists(dstFolder))
        {
            if(clearTargetDir)
                UnityEditor.FileUtil.DeleteFileOrDirectory(dstFolder);// DeleteDirectory(dstFolder);
        }
        
        CopyDirectory(srcFolder, dstFolder);
        //UnityEditor.FileUtil.CopyFileOrDirectory(srcFolder, dstFolder);

        ClearUnityFiles(dstFolder);

        Debug.Log(string.Format("同步完成:{0} -> {1}", srcFolder, dstFolder));
    }

    static void DeleteDirectory(string directoryPath)
    {
        string[] allFiles = Directory.GetFiles(directoryPath);
        
        //删除文件
        for (int i = 0; i < allFiles.Length; i++)
        {
            File.Delete(allFiles[i]);
        }

        string[] allDir = Directory.GetDirectories(directoryPath);

        if (allFiles.Length == 0 && allDir.Length == 0)
        {
            Directory.Delete(directoryPath);
            return;
        }
        //删除文件夹
        for (int i = 0; i < allDir.Length; i++)
        {
            DeleteDirectory(allDir[i]);
        }
    }
    
    static void CopyDirectory(string fromDir,string toDir)
    {
        if (!Directory.Exists(fromDir))
        {
            return;
        }
        if (!Directory.Exists(toDir))
        {
            Directory.CreateDirectory(toDir);
        }
        
        string[] directories = Directory.GetDirectories(fromDir);
        if(directories.Length > 0)
        {
            foreach(string d in directories)
            {
                CopyDirectory(d,toDir + d.Substring(d.LastIndexOf("\\")));
            }
        }
        string[] files = Directory.GetFiles(fromDir);
        if(files.Length > 0)
        {
            foreach(string s in files)
            {
                File.Copy(s,toDir + s.Substring(s.LastIndexOf("\\")),true);
            }
        }
        
    }
    
    /// <summary>
    /// 清除Unity的独有文件
    /// </summary>
    static void ClearUnityFiles(string folder)
    {
        // meta
        string[] metaFiles = Directory.GetFiles(folder, "*.meta", SearchOption.AllDirectories);
        foreach (string file in metaFiles)
        {
            File.Delete(file);
        }

        // asmdef
        string[] asmdefFiles = Directory.GetFiles(folder, "*.asmdef", SearchOption.AllDirectories);
        foreach (string file in asmdefFiles)
        {
            File.Delete(file);
        }
    }

}


#endif